package com.wsoft.cms.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.cms.entity.CmsCategoryAuthEntity;
import com.wsoft.cms.mapper.CmsCategoryAuthMapper;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author z001
 * @Date 2024/11/27 10:42
 */
@Slf4j
@Service
public class CmsCategoryAuthManager extends ServiceImpl<CmsCategoryAuthMapper, CmsCategoryAuthEntity> {

    @Resource
    private RedisService redisService;

    /**
     * 根据category删除
     * @param cateGoryId
     * @return
     */
    public boolean delByCategoryId(Long cateGoryId){
        if(null == cateGoryId){
            return false;
        }
        QueryWrapper<CmsCategoryAuthEntity> query = new QueryWrapper<>();
        query.lambda().eq(CmsCategoryAuthEntity::getCategoryId,cateGoryId);
        boolean b = remove(query);
        if(b){
            redisService.del(RedisKeyConstant.Cms.CMS_CATEGORY_AUTH_INFO + cateGoryId);
        }
        return b;
    }


    /**
     * 根据cateGoryId下的所有orgId
     * @param cateGoryId
     * @return
     */
    @GetRedisCache(type = RedisKeyConstant.Cms.CMS_CATEGORY_AUTH_INFO,
            expire_sec = RedisKeyConstant.Cms.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public List<Long> getByCategoryId(Long cateGoryId){
        QueryWrapper<CmsCategoryAuthEntity> query = new QueryWrapper<>();
        query.lambda().eq(CmsCategoryAuthEntity::getCategoryId,cateGoryId);
        List<CmsCategoryAuthEntity> list = list(query);
        //返回orgIdList
        List<Long> orgIdList = new ArrayList<>();
        for (CmsCategoryAuthEntity auth : list) {
            orgIdList.add(auth.getOrgId());
        }
        return  orgIdList;
    }

    /**
     * 根据orgId查询,返回所有的分类id
     * @param orgId
     * @return
     */
    public List<Long> getByOrgId(Long orgId){
        QueryWrapper<CmsCategoryAuthEntity> query = new QueryWrapper<>();
        query.lambda().eq(CmsCategoryAuthEntity::getOrgId,orgId);
        List<CmsCategoryAuthEntity> list = list(query);
        //返回orgIdList
        List<Long> orgIdList = new ArrayList<>();
        for (CmsCategoryAuthEntity auth : list) {
            orgIdList.add(auth.getCategoryId());
        }
        return  orgIdList;
    }
}
